//
//  TalkfunTestPaperDetailslSolution.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/1/13.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
typealias testPaperDetailslReplyBlock = () -> Void

typealias testPaperBriefReplyBlock = (_ msg:NSString) -> Void

class TalkfunTestPaperDetailslReply: UIView ,UICollectionViewDelegate, UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout, UITextViewDelegate {
    
    var optionsList = NSMutableArray()
    var isEndOfTestPaper:Bool =  false
    
    let TalkfunTestPaperDetailslReplyCell_ID = "TalkfunTestPaperDetailslReplyCell_ID"
    let collectionView = UICollectionView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: UICollectionViewFlowLayout())
    //填空题 数据提交
    var replyBlock: testPaperDetailslReplyBlock?
    //简答题 数据提交
    var briefReplyBlock: testPaperBriefReplyBlock?
    
    let textView = TF_TextView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        self.collectionView.isScrollEnabled = false
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.register(TalkfunTestPaperDetailslReplyCell.self, forCellWithReuseIdentifier:TalkfunTestPaperDetailslReplyCell_ID)
        self.addSubview(self.collectionView)
        self.collectionView.backgroundColor = UIColor.clear
        
        self.addSubview(self.textView)
        self.textView.font = UIFont.PingFang(size: 16.0)
        self.textView.tf_placeholder = "请输入"
       
        self.textView.textContainerInset = UIEdgeInsets(top: 11, left: 16, bottom: 17, right: 16)
        
      
        self.textView.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0 , alpha: 1.0)
       
        self.textView.delegate = self
        self.textView.backgroundColor = UIColor.white
        self.textView.layer.masksToBounds  = true
        self.textView.isHidden = true
        //圆角
        self.textView.layer.cornerRadius = 4;
        //边框
        self.textView.layer.borderWidth = 1;
       //边框颜色
        self.textView.layer.borderColor = UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0 , alpha: 1.0).cgColor
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.collectionView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width , height: self.frame.size.height)
        
        
        self.textView.frame = CGRect(x: 0, y: 15, width: self.frame.size.width, height: self.frame.size.height - 15 )
        
        

    }
    
    func options(list:NSMutableArray,isEndOfTestPaper:Bool)
    {

        self.isEndOfTestPaper = isEndOfTestPaper
        self.optionsList.removeAllObjects()
        self.optionsList.addObjects(from: list as! [Any])
        self.collectionView.reloadData()

    }
    //TODO: 数据源
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.optionsList.count
    }
    //TODO: cell的创建
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TalkfunTestPaperDetailslReplyCell_ID, for: indexPath) as! TalkfunTestPaperDetailslReplyCell
        cell.backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 1)
        
        let rom = NSString(format: "%i", indexPath.row + 1)
        let inxLabelMst = "(" + (rom as String) + ")"
        
        cell.inxLabel.text = inxLabelMst
        cell.content.tag = indexPath.row
        
        let model = self.optionsList[indexPath.row] as! TalkfunSubjectAnswer
        cell.content.text = model.fillInTheBlank
        cell.content.addTarget(self, action: #selector(editingDidEnd), for: UIControl.Event.editingDidEnd)
        
        
        cell.content.addTarget(self, action: #selector(editingDidEnd), for: UIControl.Event.editingDidEndOnExit)
        

        if (self.isEndOfTestPaper) {
            cell.content.isUserInteractionEnabled = false
        }else
        {
            cell.content.isUserInteractionEnabled = true
        }
       return cell
        
      
    }
    //TODO: UICollectionViewCell高度
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize
    {

        return  CGSize(width: self.frame.size.width, height: 44.0)
       
    }
    //TODO: 點選 cell 後執行的動作
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
     
      
    }
    // cell与上下间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
     
        return 15
    }

    //输入结束
    @objc func editingDidEnd(textField:UITextField)
    
    {
        if (textField.tag < self.optionsList.count) {
            let model = self.optionsList[textField.tag] as! TalkfunSubjectAnswer
            
            model.fillInTheBlank = (textField.text! as NSString) as String
        }
        if self.replyBlock != nil {
            self.replyBlock!(  )
        }
        
    }
    //
    func textViewDidBeginEditing(_ textView: UITextView) {
        self.textView.layer.borderColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0 , alpha: 1.0).cgColor
    }
    
    //在输入
    func textViewDidChange(_ textView: UITextView) {
        
        
        
    }
    //输入结束
    func textViewDidEndEditing(_ textView: UITextView) {
        self.textView.layer.borderColor = UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0 , alpha: 1.0).cgColor
        if self.briefReplyBlock != nil {
            self.briefReplyBlock!(textView.text! as NSString)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
